// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.neuronbit.businessflow.intellij.editor.swing.editor;

import com.mxgraph.model.mxCell;
import com.neuronbit.businessflow.intellij.editor.swing.XmlWriter;
import com.neuronbit.businessflow.intellij.model.*;
import org.apache.commons.lang3.NotImplementedException;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.TreeMap;

/**
 * The type Rad auto task component.
 */
public class RadStatefulTaskComponent extends RadJavaMethodComponent {
    /**
     * Instantiates a new Rad auto task component.
     *
     * @param module the module
     * @param cell   the cell
     * @param id     the id
     */
    public RadStatefulTaskComponent(final ModuleProvider module, final mxCell cell, final String id){
    super(module, cell, id);
  }

    /**
     * Instantiates a new Rad auto task component.
     *
     * @param cell    the cell
     * @param id      the id
     * @param palette the palette
     */
    public RadStatefulTaskComponent(@NotNull final mxCell cell, @NotNull final String id, final Palette palette) {
    super(null, cell, id, palette);
  }

  @Override
  public void write(final XmlWriter writer) {
    throw new NotImplementedException();
//    writer.startElement("component");
//    try{
//      writeId(writer);
//      writeClass(writer);
//      writeBinding(writer);
//      writeConstraints(writer);
//      writeProperties(writer);
//    }finally{
//      writer.endElement(); // component
//    }
  }


    /**
     * Gets param value.
     *
     * @param index the index
     * @return the param value
     */
    public String getStateCheckerParamValue(String index) {
        StatefulTask task = (StatefulTask) getDelegee().getValue();
        return task.getStateChecker().getParamValue(index);
    }

    /**
     * Sets param value.
     *
     * @param index the index
     * @param value the value
     */
    public void setStateCheckerParamValue(String index, String value) {
        StatefulTask task = (StatefulTask) getDelegee().getValue();
        task.getStateChecker().setParam(index, value);
    }

    /**
     * Gets exception handler class to bind.
     *
     * @return the exception handler class to bind
     */
    public String getStateCheckerClassToBind() {
        if (getDelegee().getValue() == null) {
            return null;
        }

        StatefulTask task = (StatefulTask) getDelegee().getValue();
        Type type = task.getStateChecker().getType();
        if (null == type) {
            return "";
        }
        return type.getQualifiedName();
    }

    /**
     * Sets exception handler class to bind.
     *
     * @param classToBind the class to bind
     */
    public void setStateCheckerClassToBind(String classToBind) {
        if (getDelegee().getValue() == null) {
            return;
        }

        StatefulTask task = (StatefulTask) getDelegee().getValue();
        task.getStateChecker().setType(SimpleType.of(classToBind));
    }

    /**
     * Gets exception handler method to bind.
     *
     * @return the exception handler method to bind
     */
    public String getStateCheckerMethodToBind() {
        if (getDelegee().getValue() == null) {
            return null;
        }

        StatefulTask task = (StatefulTask) getDelegee().getValue();
        FormattedMethodString method = task.getStateChecker().getMethod();
        if (method == null) {
            return null;
        }
        return method.getValue();
    }

    /**
     * Sets exception handler method to bind.
     *
     * @param methodToBind the method to bind
     */
    public void setStateCheckerMethodToBind(String methodToBind) {
        if (getDelegee().getValue() == null) {
            return;
        }

        FormattedMethodString method = new FormattedMethodString(methodToBind);

        StatefulTask task = (StatefulTask) getDelegee().getValue();

        Call callable = task.getStateChecker();
        callable.setMethod(method);

        Map<String, Param> params = callable.getParams();
        Map<String, Param> oldParams = new TreeMap<>(params);
        params.clear();

        String[] parameterTypes = method.getParameterTypes();
        for (int i = 0; i < parameterTypes.length; i++) {
            Param param = oldParams.get(String.valueOf(i));
            if (param != null) {
                params.put(String.valueOf(i), param);
            } else {
                params.put(String.valueOf(i), new Param(String.valueOf(i), ""));
            }
        }
    }
}
